//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BlackKarasinski.h"
using namespace Cephei::QL::Models::Shortrate::Onefactormodels;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/TimeGrid.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/ShortRateModel.h>
#include <gen/QL/Models/TermStructureConsistentModel.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::CBlackKarasinski (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma) : CTermStructureConsistentModel(CBlackKarasinski::typeid)
{
    CYieldTermStructure^ _CtermStructure;
    try
    {
#ifdef HANDLE
        _phBlackKarasinski = NULL;
#endif
        _CtermStructure = safe_cast<CYieldTermStructure^> (termStructure);
        _CtermStructure->Lock();
        Handle<QuantLib::YieldTermStructure>& _termStructure = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CtermStructure->GetHandle ()); 
        QuantLib::Real _a = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (a) ? (QuantLib::Real)ValueHelper::Convert (a->Value) : 0.1); //4
        QuantLib::Real _sigma = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (sigma) ? (QuantLib::Real)ValueHelper::Convert (sigma->Value) : 0.1); //4
        _ppBlackKarasinski = new boost::shared_ptr<QuantLib::BlackKarasinski> (new QuantLib::BlackKarasinski ( _termStructure,  _a,  _sigma ));
        SetTermStructureConsistentModel (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppBlackKarasinski));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CtermStructure != nullptr) _CtermStructure->Unlock();
    }
}
Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::CBlackKarasinski (boost::shared_ptr<QuantLib::BlackKarasinski>& childNative, Object^ owner) : CTermStructureConsistentModel(CBlackKarasinski::typeid)
{
#ifdef HANDLE
	_phBlackKarasinski = NULL;
#endif
	_ppBlackKarasinski = &childNative;
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppBlackKarasinski));
}
Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::CBlackKarasinski (QuantLib::BlackKarasinski& childNative, Object^ owner) : CTermStructureConsistentModel(CBlackKarasinski::typeid)
{
#ifdef HANDLE
	_phBlackKarasinski = NULL;
#endif
	_ppBlackKarasinski = new boost::shared_ptr<QuantLib::BlackKarasinski> (&childNative);
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppBlackKarasinski));
    _BlackKarasinskiOwner = owner;
    _TermStructureConsistentModelOwner = owner;
}

Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::CBlackKarasinski (CBlackKarasinski^ copy) : CTermStructureConsistentModel(CBlackKarasinski::typeid)
{
#ifdef HANDLE
	_phBlackKarasinski = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackKarasinski = new boost::shared_ptr<QuantLib::BlackKarasinski> (copy->GetShared());
        _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppBlackKarasinski));
    }
}
Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::CBlackKarasinski (System::Type^ t) : CTermStructureConsistentModel(CBlackKarasinski::typeid)
{
#ifdef HANDLE
	_phBlackKarasinski = NULL;
#endif
	if (!t->IsSubclassOf(CBlackKarasinski::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::CBlackKarasinski (QuantLib::Handle<QuantLib::BlackKarasinski>& childNative, Object^ owner)  : CTermStructureConsistentModel(CBlackKarasinski::typeid)
{
	_phBlackKarasinski = &childNative;
	_ppBlackKarasinski = &static_cast<boost::shared_ptr<QuantLib::BlackKarasinski>>(childNative.currentLink());
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppBlackKarasinski));
    _BlackKarasinskiOwner = owner;
}
Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::CBlackKarasinski (QuantLib::Handle<QuantLib::BlackKarasinski> childNative)  : CTermStructureConsistentModel(CBlackKarasinski::typeid)
{
	_phBlackKarasinski = &childNative;
	_ppBlackKarasinski = &static_cast<boost::shared_ptr<QuantLib::BlackKarasinski>>(childNative.currentLink());
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppBlackKarasinski));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::CBlackKarasinski (QuantLib::BlackKarasinski childNative)  : CTermStructureConsistentModel(CBlackKarasinski::typeid)
{
#ifdef HANDLE
	_phBlackKarasinski = NULL;
#endif
	_ppBlackKarasinski = new boost::shared_ptr<QuantLib::BlackKarasinski> (new QuantLib::BlackKarasinski (childNative));
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppBlackKarasinski));
}
#endif

Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::~CBlackKarasinski ()
{
    if (_ppBlackKarasinski != NULL)
    {
	    delete _ppBlackKarasinski;
        _ppBlackKarasinski = NULL;
    }
}
Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::!CBlackKarasinski ()
{
    if (_ppBlackKarasinski != NULL)
    {
	    delete _ppBlackKarasinski;
    }
}
QuantLib::BlackKarasinski& Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::GetReference ()
{
    if (_ppBlackKarasinski == NULL) throw gcnew NativeNullException ();
	return **_ppBlackKarasinski;
}
boost::shared_ptr<QuantLib::BlackKarasinski>& Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::GetShared ()
{
    if (_ppBlackKarasinski == NULL) throw gcnew NativeNullException ();
	return *_ppBlackKarasinski;
}
QuantLib::BlackKarasinski* Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::GetPointer ()
{
    if (_ppBlackKarasinski == NULL) throw gcnew NativeNullException ();
	return &**_ppBlackKarasinski;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackKarasinski>& Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::GetHandle ()
{
	if (_phBlackKarasinski == NULL)
	{
		_phBlackKarasinski = new Handle<QuantLib::BlackKarasinski> (*_ppBlackKarasinski);
	}
	return *_phBlackKarasinski;
}
#endif
bool Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::HasNative () 
{
	return (_ppBlackKarasinski != NULL);
}

Cephei::QL::ILattice^ Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski::Tree (Cephei::QL::ITimeGrid^ grid)
{
    CTimeGrid^ _Cgrid;
    try
    {
        _Cgrid = safe_cast<CTimeGrid^> (grid);
        _Cgrid->Lock();
        QuantLib::TimeGrid& _grid = static_cast<QuantLib::TimeGrid&> (_Cgrid->GetReference ()); 
    	boost::shared_ptr<QuantLib::Lattice> _rv = (boost::shared_ptr<QuantLib::Lattice>)(*_ppBlackKarasinski)->tree ( _grid );   
        Cephei::QL::CLattice^ _nrv = gcnew Cephei::QL::CLattice (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrid != nullptr) _Cgrid->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Shortrate::Onefactormodels::IBlackKarasinski^ Cephei::QL::Models::Shortrate::Onefactormodels::CBlackKarasinski_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma)
{
    return gcnew CBlackKarasinski ( termStructure,  a,  sigma);
}
